﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;
using Tencent.OA.App;
using System.Web.Routing;
using Tencent.OA.ACM.Entity;

namespace Tencent.OA.ACM.App
{
    /// <summary>
    /// 定义一组公共的通用的扩展方法。
    /// </summary>
    public static class CommonExtensions
    {

        #region 格式化时间
        public static string FormatDatetime(this HtmlHelper html, DateTime? datetime)
        {
            return FormatDatetime(html, datetime, System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat);
        }
        public static string FormatDatetime(this HtmlHelper html, DateTime? datetime, string format)
        {
            return FormatDatetime(html, datetime, format, System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat);
        }

        public static string FormatDatetime(this HtmlHelper html, DateTime? datetime, IFormatProvider provider)
        {
            return FormatDatetime(html, datetime, null, provider);
        }

        public static string FormatDatetime(this HtmlHelper html, DateTime? datetime, string format, IFormatProvider provider)
        {
            if (datetime.HasValue)
                return FormatDatetime(html, datetime.Value, format, provider);
            return "";
        }

        public static string FormatDatetime(this HtmlHelper html, DateTime datetime)
        {
            return datetime.ToString();
        }
        public static string FormatDatetime(this HtmlHelper html, DateTime datetime, string format)
        {
            return datetime.ToString(format);
        }
        public static string FormatDatetime(this HtmlHelper html, DateTime datetime, IFormatProvider provider)
        {
            return FormatDatetime(html, datetime, null, provider);
        }

        public static string FormatDatetime(this HtmlHelper html, DateTime datetime, string format, IFormatProvider provider)
        {
            return datetime.ToString(format, provider);
        }
        public static string FormatDateTimeShortDateString(this HtmlHelper html, DateTime? datetime)
        {
            if (datetime == null)
                return string.Empty;
            else
                return datetime.Value.ToShortDateString();
        }
        #endregion

        /// <summary>
        /// 当指定的数据列表为空或没有数据行时显示一行“还没有添加记录！”的文本。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="list"></param>
        /// <param name="tdCount">表格的列(TD)数</param>
        /// <returns></returns>
        public static string ListTableNoResultMessage<T>(this HtmlHelper htmlHelper, IList<T> list, int tdCount)
        {
            return ListTableNoResultMessage(htmlHelper, list, tdCount, "还没有相关记录！");
        }

        public static string ListTableNoResultMessage<T>(this HtmlHelper htmlHelper, IList<T> list, int tdCount, string emptyRowText)
        {
            if (list == null || list.Count == 0)
                return string.Format("<tr class=\"noresult\"><td align=\"center\"{0}>{1}</td></tr>", 
                    (tdCount > 1 ?  (" colspan=\"" + tdCount + "\"") : ""), emptyRowText);
            return "";
        }
        /// <summary>
        /// 限制字符串长度输出
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="str"></param>
        /// <param name="limitLength"></param>
        /// <returns></returns>
        public static string LimitStringLength(this HtmlHelper htmlHelper, string str, int limitLength)
        {
            if (string.IsNullOrEmpty(str))
                return str;

            if (str.Length <= limitLength)
                return str;
            else
                return str.Substring(0, limitLength - 3) + "...";
        }

        /// <summary>
        /// 如果是空则显示横线
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string IfNullLine(this string str)
        {
            if (string.IsNullOrEmpty(str))
                return "-";
            else
                return str;
        }
    }
}
